package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.MasterBean;
import cst.a931xmc.yuejian20.Interface.ItemIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.HomeService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MasterModel<T> extends ApiModel implements ItemIface {
    private Retrofit retrofit;
    private HomeService service;

    public MasterModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(HomeService.class);

    }
    @Override
    public void getList(final Listener l) {
        Call<List<MasterBean>> call = service.getMasterList();
        call.enqueue(new Callback<List<MasterBean>>() {
            @Override
            public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<MasterBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int master_id, final Listener l) {
        Call<MasterBean> call = service.getMasterDetail(master_id);
        call.enqueue(new Callback<MasterBean>() {
            @Override
            public void onResponse(Call<MasterBean> call, Response<MasterBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<MasterBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
